package com.hb.back.config;

import com.hb.back.filter.TokenFile;
import com.hb.back.service.impl.EduAclUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig {
    @Autowired
    EduAclUserServiceImpl aclUserService;

    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

   @Bean
   WebSecurityCustomizer webSecurityCustomizer(){
         return web -> web.ignoring().requestMatchers("/centos/**","/video/**","/image/**","/teacher/seave","/page","/user/info","/v3/**","/webjars/**","/doc.html", "/swagger-ui/**", "/v3/api-docs/**","/user/login","/swagger-ui.html");
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity security) throws Exception {
    security.csrf().disable();
    security.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    security.authorizeHttpRequests().anyRequest().authenticated();
    security.userDetailsService(aclUserService);
    security.addFilterBefore(new TokenFile(aclUserService), UsernamePasswordAuthenticationFilter.class);
    return security.build();
    }
}
